package com.david.IfaceAutoTest.controller;

import com.david.IfaceAutoTest.domain.TableDomain;
import com.david.IfaceAutoTest.entity.Interface;
import com.david.IfaceAutoTest.mapper.InterfaceMapper;
import com.david.IfaceAutoTest.service.InterfaceService;
import com.david.IfaceAutoTest.utils.ExportExcelUtil;
import com.david.IfaceAutoTest.utils.MapTools;
import io.swagger.annotations.*;
import com.david.IfaceAutoTest.entity.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 控制层
 *
 * @author david
 * @since 2021-12-14
 */
@RestController
@Api(tags = "interface接口")
@RequestMapping("/IfaceAutoTest/interface")
public class InterfaceController {

    private final InterfaceService interfaceservice;

    @Autowired
    public InterfaceController(InterfaceService interfaceservice) {
        this.interfaceservice = interfaceservice;
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表查询", notes = "list")
    public CommonResult<Object> list() {
        return CommonResult.success(interfaceservice.list());
    }

    @GetMapping("/get")
    @ApiOperation(value = "详情查询", notes = "get")
    public CommonResult<Object> get(Integer id) {
        return CommonResult.success(interfaceservice.getById(id));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存", notes = "save")
    public CommonResult<Object> save(Interface iface) {
        return CommonResult.success(interfaceservice.save(iface));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除", notes = "delete")
    public CommonResult<Object> delete(Integer id) {
        interfaceservice.removeById(id);
        return CommonResult.success();
    }

    @GetMapping("/run")
    @ApiOperation(value = "接口执行", notes = "run")
    public CommonResult<Object> run(Integer id) {
        return CommonResult.success(interfaceservice.run(id));
    }

    @GetMapping("/getTableDomain")
    @ApiOperation(value = "接口表明细获取", notes = "run")
    public CommonResult<Object> getss(Integer id) {
        List<TableDomain> tableDomains= interfaceservice.getTableDomain("interface");
        return CommonResult.success(tableDomains);
    }

    @GetMapping(value = "/alarm/export")
    public CommonResult<Object> exportAlarmEvent(HttpServletResponse response)  throws Exception{
        //可以获取到无序含列名结果
        List<List<Object>> allList = new ArrayList<List<Object>>();
        List<Map<String, Object>> map = interfaceservice.getTest();
        int index = 0;
        for (Map<String, Object> kv : map) {
            List<Object> key = new ArrayList<Object>();
            List<Object> value = new ArrayList<Object>();
            for (Map.Entry<String, Object> entry : kv.entrySet()) {
                if (index == 0) {
                    key.add(entry.getKey());
                }
                value.add(entry.getValue());
            }
            if (index == 0) {
                allList.add(key);
            }
            allList.add(value);
            index++;
        }
        // 调用你的服务，获取数据源
        List<Interface>dataLists = interfaceservice.list();
        //TODO 获取有序表头、与查询结果对应。 或者定好表头编写已表头为基准的定制化查询方法。
        List<TableDomain> tableDomains= interfaceservice.getTableDomain("interface");

//        获取entity 键值对，进而获取字段名-转化成excel列名
        Map<String,Object> getTitleMap= MapTools.objectToMap(dataLists.get(0));

        //通过反射获取实体字段信息
        Field[] fields = Interface.class.getDeclaredFields();
        System.out.println(tableDomains);
        if (dataLists != null && dataLists.size() > 0) {


            List<List<Interface>> dd= Collections.singletonList(dataLists);
            try {
                // Sheet页名称列表
                List<String> sheetNames = new ArrayList<String>();
                sheetNames.add("jiekou");
                // 一个Sheet页表头名称列表
                List<String > headers = tableDomains.stream().map(TableDomain::getFieldDesc).collect(Collectors.toList());                // 模拟多个Sheet页表头
                List<List<String>> headersList = Arrays.asList(headers, headers, headers);
                // 多个Sheet页标题列表(若)
                List<String> titleList = new ArrayList<String>();
                titleList.add("接口列表");
                ExportExcelUtil
                        .exportExcel("Excel文件名", sheetNames, titleList, headersList, dd, response, null, true);
            } catch (Exception e) {
                return CommonResult.success("导出告警事件出错"+ e);
            }
        }
        return CommonResult.success("导出成功");
    }
}
